package MySql;

import java.sql.*;
import javax.swing.*;

public class mySql_Driver {
    Connection con = null;
    Statement stmt = null;
    int count = 0 ;
    //连接
    public void Connect(){
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //打开数据库
            String user = "root";
            String password = "000528";
            String url = "jdbc:mysql://localhost:3306/cms?useSSL=false";
            con = DriverManager.getConnection(url,user,password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    //从数据库中读取
//    public User[] read() throws SQLException {
//        Connect();
//        User []users = new User[1024];
//        count = 0;
//        //创建数据库对象
//        stmt = con.createStatement();
//        String sql = "SELECT userId,passwd,answer1,answer2 FROM member;";
//        ResultSet rs = stmt.executeQuery(sql);
//        while(rs.next()){
//            String userId = rs.getString("userId");
//            String passwd = rs.getString("passwd");
//            String answer1 = rs.getString("answer1");
//            String answer2 = rs.getString("answer2");
//            User user = new User(userId,passwd,answer1,answer2);
//            users[++count]=user;
//        }
//        con.close();
//        rs.close();
//        stmt.close();
//        return users;
//    }
    public int getCount(){
        return count;
    }
    //修改数据库
    public void changePassword(String id,String password) {
        Connect();
        String sql ="update member set passwd = ? where userId = ?";
        try {
            PreparedStatement Stmt= con.prepareStatement(sql);
            Stmt.setString(1,password);
            Stmt.setString(2,id);
            Stmt.executeUpdate();
            con.close();
            Stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
